import cv2

from ultralytics import solutions

cap = cv2.VideoCapture("video/192.168.31.196_01_20250415163619225_2.mp4")
assert cap.isOpened(), "Error reading video file"

# region_points = [(20, 400), (1080, 400)]                     [(15,1800),(3800,1800)]                 # line counting
region_points = [(20, 1800), (3800, 1800), (3800, 1900), (20, 1900)]  # rectangle region
# region_points = [(20, 400), (1080, 400), (1080, 360), (20, 360), (20, 400)]   # polygon region

# Video writer
# w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
# video_writer = cv2.VideoWriter("object_counting_output.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))

# Initialize object counter object
counter = solutions.ObjectCounter(
    show=False,  # display the output
    region=region_points,  # pass region points
    model="model2/yolo11s.pt",  # model="yolo11n-obb.pt" for object counting with OBB model.
    classes=[0],  # count specific classes i.e. person and car with COCO pretrained model.
    # tracker="botsort.yaml",  # choose trackers i.e "bytetrack.yaml"
    verbose=False
)

# Process video
while cap.isOpened():
    success, im0 = cap.read()

    if not success:
        print("Video frame is empty or processing is complete.")
        break

    results = counter(im0)

    print(results)  # access the output

    # video_writer.write(results.plot_im)  # write the processed frame.

cap.release()
# video_writer.release()
# cv2.destroyAllWindows()  # destroy all opened windows